package com.smec.moblie.util;

import android.content.Context;
import android.os.Environment;

import com.smec.moblie.common.ConstValue;

import java.io.File;

/**
 * 照片上传
 */
public class ConfigContext {
    private static ConfigContext configContext;

    public static final String UPLOAD_LIST_SHARED_PREFERENCE_NAME = "SMEC_REMES_MOBILE_UPLOAD_LIST";

    private File storeFilesDir;
    private File storeCacheDir;
    private static String imgPath = Environment.getExternalStorageDirectory() + "/DCIM/Camera/";

    private ConfigContext(Context context) {
        init(context);
    }

    public static ConfigContext getInstance() {
        synchronized (ConfigContext.class) {
            if (configContext == null) {
                throw new RuntimeException("Not init yet! ");
            }
        }
        return configContext;
    }

    public static void newInstance(Context context) {
        configContext = new ConfigContext(context);
    }

    public void refresh(Context context) {
        configContext = new ConfigContext(context);
    }

    private void init(Context context) {
        initDirs(context);
    }

    private void initDirs(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())
                && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment
                .getExternalStorageState())) {
            storeFilesDir = context.getExternalFilesDir(null);

            storeFilesDir = new File(Environment.getExternalStorageDirectory()
                    + ConstValue.PICTUREPATH);
            if (storeFilesDir.exists()) {
                storeFilesDir.delete();
            } else {

            }

            storeFilesDir.mkdirs();

            storeCacheDir = context.getExternalCacheDir();
        } else {
            storeFilesDir = context.getFilesDir();
            storeCacheDir = context.getCacheDir();
        }
        if (!storeFilesDir.exists()) {
            storeFilesDir.mkdirs();
        }
        if (!storeCacheDir.exists()) {
            storeCacheDir.mkdirs();
        }

        imgPath = storeFilesDir.getAbsolutePath() + "/pictures/";
    }

    /**
     * @return the imgPath
     */
    public static String getImgPath() {
        return imgPath;
    }

    public File getStoreFilesDir() {
        return new File(storeFilesDir.getAbsolutePath());
    }

    public File getStoreCacheDir() {
        return new File(storeCacheDir.getAbsolutePath());
    }
}

